package com.itheima.web.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.User;
import com.itheima.service.UserService;
import com.itheima.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/UserServlet")
public class UserServlet extends BaseServlet {

    // 模板代码
    protected void tempalte(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    //创建成员变量
    //1.service层实现类对象
    private UserService userService = new UserServiceImpl();
    //2.ObjectMapper对象
    private ObjectMapper objectMapper = new ObjectMapper();

    //分页查询
    protected void findByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取前端传入的参数
        String pageNumStr = request.getParameter("pageNum");
        String pageSizeStr = request.getParameter("pageSize");
        //2.判空操作
        if (pageNumStr == null || pageNumStr ==""){
            pageNumStr = "1";
        }
        if (pageSizeStr == null || pageSizeStr ==""){
            pageSizeStr = "5";
        }
        //3.转为整型
        int pageNum = Integer.parseInt(pageNumStr);
        int pageSize = Integer.parseInt(pageSizeStr);
        //4.调用service层
        PageInfo<User> pageInfo = userService.findByPage(pageNum,pageSize);
        //5.调用BaseServlet层的方法
        new BaseServlet().writeJsonToClient(pageInfo,response);
    }
    //添加学生
    protected void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取前端传入的参数,并转为json格式
        User user = objectMapper.readValue(request.getInputStream(), User.class);
        //2.调用Service层
        userService.save(user);
        //3.(设置响应的字符形式)向客户端发送请求
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write("ok");
    }
    //删除学生
    protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取前端传入的id
        String id = request.getParameter("id");
        //2.调用service层
        userService.deleteUser(id);
        //3.(设置响应的字符形式)向客户端发送请求
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write("ok");
    }

    //更新学生
    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取前端传入的对象,并转为对象形式
        User user = objectMapper.readValue(request.getInputStream(), User.class);
        //2.调用service层对象
        userService.update(user);
        //3.(设置响应的字符形式)向前端返回数据
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write("ok");


    }

}